local locplayer = ac.player.self
local back_image = [[ui\聊天\聊天框整体底板.tga]]
local slider_image1 = [[ui\聊天\滚轮底部.tga]]
local slider_image2 = [[ui\聊天\滑轮b.tga]]
local slider_strip = [[ui\聊天\滚轮条.tga]]

local btn_images_1 = {
    [[ui\聊天\llan1b.tga]],
    [[ui\聊天\llan2b.tga]],
    [[ui\聊天\llan3b.tga]],
}
local btn_images_2 = {
    [[ui\聊天\llan1a.tga]],
    [[ui\聊天\llan2a.tga]],
    [[ui\聊天\llan3a.tga]],
}
local highlight_images = {
    [[ui\聊天\向下缩放a.tga]],
    [[ui\聊天\向下缩放b.tga]],
    [[ui\聊天\向下缩放c.tga]],
    [[ui\聊天\向下缩放d.tga]],
}



local ui = class.button.create(back_image,0,600,500,300)
ui:set_ignore_trackevents(false)
ui.fixed_y = ui.y + ui.h
ui.show_panel = ui:add_panel('',15,15,ui.w-80,ui.h*0.9)
ui.show_panel.ex_scroll = 0
ui.show_panel:set_view_port(true)

ui.scale_button = ui:add_button(highlight_images[1],ui.w-80,15,36,36)
ui.scale_button:set_highlight(highlight_images[2])

ui.slider = ui:add_slider(ui.w-40,15,25,ui.show_panel.h)
ui.slider.bind_panel = ui.show_panel
ui.slider.strip_panel = ui.slider.child1:add_panel(slider_strip,6,0,ui.slider.w-12,ui.slider.h)

ui.slider.child3:set_level(9)
ui.slider:set_normal_image(slider_image1)
ui.slider:set_thumb_image(slider_image2)


ac.game:event '本地-鼠标滚动'(function(_,x,y)
    return ui:is_show() and ui:has_point(x,y)
end)

function ui.slider:on_slider_step_changed(rate)
    self.strip_panel:set_height((self.h-self.child1.h)*rate + self.child1.h/2)
end


--刷新数据
function ui:update(chat_class)
    local key = ('%s消息'):format(chat_class or self.show_class)
    local info = locplayer:get_data(key) or {}
    local x,y = 0,20

    for a,text in ipairs(self.texts) do
        local str = info[a]
        if str then
            text:set_text(str)
            text.h = text:get_height(text)
            text:set_position(x,y)
            text:show()
            y = y + text.h
        else
            text:hide()
        end
    end
    print(self.show_panel:get_child_max_y(),self.show_panel.h)
    self.slider:set_value(self.slider.max_value)
    self:on_button_mouse_enter()
end

--进入
function ui:on_button_mouse_enter()
    self:set_alpha(1)
    if self.show_panel:get_child_max_y()<self.show_panel.h then
        self.slider:hide()
    else
        self.slider:show()
    end
    self.scale_button:show()
    self.show_time = 5
end


--离开
ui.show_time = 1
--
ac.hardware:event '界面绘制'(function(_,frame)
    if ui.show_time>0 then
        local x,y = ac.hardware:get_mouse_pos()
        if ui:has_point(x,y)==false then
            ui.show_time = ui.show_time - frame
            if ui.show_time<=0 then
                ui:set_alpha(0.5)
                ui.slider:hide()
                ui.scale_button:hide()
            end
        end
    end
end)

--点击
function ui:on_button_clicked(button)
    if button==nil then
        return
    end
    if button.name then
        self.show_class = button.name
        if self.select_button then
            self.select_button:set_normal_image(self.select_button.image1)
            --self.select_button.title:set_color(0xffffffff)
        end
        self.select_button = button
        self.select_button:set_normal_image(button.image2)
        --self.select_button.title:set_color(0xff00ff00)
        self:update()
    elseif button==self.scale_button then
        self.scale_open = not self.scale_open
        if self.scale_open then
            self.scale_button:set_normal_image(highlight_images[3])
            self.scale_button.highlight:set_normal_image(highlight_images[4])
            self:set_height(self.h-150)
            self:set_position(self.x,self.fixed_y - self.h)
            self.show_panel:set_height(self.show_panel.h-150)
            self.slider:set_height(self.slider.h-150)
        else
            self.scale_button:set_normal_image(highlight_images[1])
            self.scale_button.highlight:set_normal_image(highlight_images[2])
            self:set_height(300)
            self:set_position(self.x,self.fixed_y - self.h)
            self.show_panel:set_height(self.show_panel.h+150)
            self.slider:set_height(self.slider.h+150)
        end
    end
end



--内容
ui.texts = {}
for a=1,100 do
    local text = ui.show_panel:add_text('文本',0,0,nil,0,12,4)
    --自动换行
    text:set_height(0)
    ui.texts[a] = text
end


--目录
local show_name = {'全部','玩家','系统'}
for a,name in ipairs(show_name) do
    local image1 = btn_images_1[a]
    local image2 = btn_images_2[a]
    local button = ui:add_button(image1,(a-1)*80,-30,72,30)
    button.name = name
    button.image1 = image1
    button.image2 = image2

    -- button.text = button:add_text(name,0,0,nil,nil,12,5)
    -- button.text:set_color(0xff999999)
    if a==1 then
        ui:on_button_clicked(button)
    end
end



local function player_add_str(player,chat_class,text)
    local key = ('%s消息'):format(chat_class)
    local info = player:get_data(key) or {}
    player:set_data(key,info)
    table.insert(info,text)
    if #info>100 then
        table.remove(info,1)
    end
end


ac.game:event '玩家-添加聊天消息'(function(_,player,chat_class,text)
    player_add_str(player,chat_class,text)
    if chat_class~='全部' then
        player_add_str(player,'全部',text)
    end
    if player:is_self() then
        ui:update()
    end
end)


ac.game:event '玩家-聊天'(function(_,player,text)
    player:notify('玩家-添加聊天消息',player,'系统','发布了一条消息')
    player:notify('玩家-添加聊天消息',player,'玩家',text)
end)

local player = ac.player[1]
for a=1,0 do
    player:notify('玩家-添加聊天消息',player,'系统','发布了一条消息'..a)
end
local a = 0
-- ac.loop(1000,function()
--     a = a + 1
--     player:notify('玩家-添加聊天消息',player,'系统','发布了一条消息'..a)
-- end)